Capacity to Transfer Packages

Medium

Question

Given a list of weights of packages you need to transfer through a conveyer belt and a number of available baskets to hold the packages in, return the minimum capacity of the baskets for all packages to be safely transferred.

Each package must be put into baskets the same order they came in. For example, if the package weights are: [1, 2, 3, 4], the packages can't be put into baskets like this: [1, 3], [2, 4].

Input: weights = [1, 3, 2, 5], baskets = 2

Output: 6

The packages would be put into 2 baskets in this way:

  • [1, 3, 2] (total weight of 6)
  • [5] (total weight of 5)

The minimum capacity for the baskets is 6.

Input: weights = [3, 2, 5, 8, 6, 2], baskets = 3

Output: 10

The packages would be put into 3 baskets in this way:

  • [3, 2, 5] (total weight of 10)
  • [8] (total weight of 8)
  • [6, 2] (total weight of 8)

The minimum capacity for the baskets is 10.

Input: weights = [5, 3, 1, 2, 4], baskets = 2

Output: 8

The packages would be put into 2 baskets in this way:

  • [5, 3] (total weight of 8)
  • [1, 2, 4] (total weight of 7)

The minimum capacity for the baskets is 8.

Clarify the problem

What are some questions you'd ask an interviewer?

Understand the problem

What is the minimum capacity for the baskets if given these inputs? weights = [4, 3, 1, 5, 2], baskets = 3
4
5
6
7

Login or signup to save your code.

Notes